unit uPrincipal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Menus, pngimage, ComCtrls, jpeg, inifiles, ToolWin,
  ImgList, ActnList, XPStyleActnCtrls, ActnMan, ActnCtrls, ActnMenus,
  StdCtrls;

type
  TFrmPrincipal = class(TForm)
    MainMenu1: TMainMenu;
    Cadastro1: TMenuItem;
    Consultas1: TMenuItem;
    Relatorios1: TMenuItem;
    Configurao1: TMenuItem;
    Ajuda1: TMenuItem;
    IcoNet: TImage;
    IcoText: TImage;
    IcoAgenda: TImage;
    IcoMail: TImage;
    IcoCliente: TImage;
    StatusBar1: TStatusBar;
    este00011: TMenuItem;
    este00021: TMenuItem;
    este00031: TMenuItem;
    este00041: TMenuItem;
    Image1: TImage;
    VerticalEsquerda: TMenuItem;
    VerticalDireita: TMenuItem;
    HorizontalTopo1: TMenuItem;
    HorizontalBase1: TMenuItem;
    procedure IcoAgendaMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure IcoAgendaMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure IcoAgendaMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure IcoClienteDblClick(Sender: TObject);
    procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure VerticalEsquerdaClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure IcoClienteClick(Sender: TObject);
    procedure VerticalDireitaClick(Sender: TObject);
    procedure HorizontalTopo1Click(Sender: TObject);
    procedure HorizontalBase1Click(Sender: TObject);
  private
    { Private declarations }
    Posicao : TPoint;
    Capturando : boolean;
    F : TINIFILE;
    procedure FormIconVertical;
    procedure FormIconHorizontal;
    procedure FormAlignIcon(AAlign: TAlign);
  public
    { Public declarations }
  end;

var
  FrmPrincipal: TFrmPrincipal;

implementation

{$R *.dfm}

procedure TFrmPrincipal.IcoAgendaMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  if Capturando then
  begin
    If ((Sender as  Timage).Left -(Posicao.x-x) < (Self.ClientWidth - (Sender as TImage).Width)) and ((Sender as  Timage).Left -(Posicao.x-x) > 0) then
        (Sender as  Timage).Left := (Sender as  Timage).Left -(Posicao.x-x);

    If ((Sender as  Timage).top -(Posicao.y-y) < (Self.Clientheight)-(Sender as TImage).Height - 20) and ((Sender as  Timage).top -(Posicao.y-y) > 0) then
        (Sender as  Timage).Top := (Sender as  Timage).Top - (Posicao.Y-y);
  end;
end;

procedure TFrmPrincipal.IcoAgendaMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if ( ssCtrl in Shift ) then
  begin
    Capturando := true;
    Posicao.X := x;
    Posicao.Y := Y;
  end;
end;


procedure TFrmPrincipal.IcoAgendaMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  left, top : integer;
begin
  if Capturando and ( ssCtrl in Shift ) then
  begin
    Capturando := false;
    If ((Sender as  Timage).Left -(Posicao.x-x) < (Self.ClientWidth - (Sender as TImage).Width)) and ((Sender as  Timage).Left -(Posicao.x-x) > 0) then
        (Sender as  Timage).Left := (Sender as  Timage).Left - (Posicao.x -x);

    If ((Sender as  Timage).top -(Posicao.y-y) < (Self.Clientheight - (Sender as TImage).height)- 20) and ((Sender as  Timage).top -(Posicao.y-y) > 0) then
        (Sender as  Timage).Top := (Sender as  Timage).Top - (Posicao.y - y);
    left := (Sender as  Timage).Left;
    top := (Sender as  Timage).Top;
    F := Tinifile.Create('c:\Icones.ini');
    F.Writeinteger((Sender as  Timage).Name ,'Left',Left);
    F.WriteInteger((Sender as  Timage).Name,'top',top);
    F.Free;

    Abort;
  end;
end;

procedure TFrmPrincipal.FormAlignIcon(AAlign: TAlign);
var
  I, J, X, Y: integer;
  lastPos: TPoint;
begin
  DoubleBuffered := true;

  J := 1;

  case AAlign of
    alLeft:
    begin
      X := 16;
      Y := 16;
    end;

    alRight:
    begin
      X := Self.ClientWidth - 16;
      Y := 16;
    end;

    alTop:
    begin
      X := 16;
      Y := 16;
    end;

    alBottom:
    begin
      X := 16;
      Y := Self.ClientHeight - 30;
    end;
  end;

  lastPos.X := X;
  lastPos.Y := Y;

  for i := 0 to Self.ComponentCount - 1 do
    if self.Components[i] is Timage then
    begin
      case AAlign of
        alLeft:
        begin
          lastPos.X := (self.Components[i] as Timage).Width;
          lastPos.Y := Y + (self.Components[i] as Timage).Height * J;
        end;

        alRight:
        begin
          lastPos.X := X - (self.Components[i] as Timage).Width;
          lastPos.Y := Y + (self.Components[i] as Timage).Height * J;
        end;

        alTop:
        begin
          lastPos.X := X + (self.Components[i] as Timage).Width * J;
          lastPos.Y := Y;
        end;

        alBottom:
        begin
          lastPos.X := X + (self.Components[i] as Timage).Width * J;
          lastPos.Y := Y - (self.Components[i] as Timage).Height;
        end;
      end;

      (self.Components[i] as Timage).Left := lastPos.X;
      (self.Components[i] as Timage).Top  := lastPos.Y;
      inc(j);
    end;
end;

procedure TFrmPrincipal.FormIconVertical;
var
  I, J: integer;
  lastPos: TPoint;
begin
  DoubleBuffered := true;

  J := 1;
  lastPos.X := 16;
  lastPos.Y := 16;

  for i:=0 to Self.ComponentCount - 1 do
    if self.Components[i] is Timage then
    begin
      lastPos.Y := ( (self.Components[i] as Timage).Height ) * J ;
      (self.Components[i] as Timage).Left := lastPos.X;
      (self.Components[i] as Timage).Top  := lastPos.Y;
      inc(j);
    end;
end;

procedure TFrmPrincipal.FormIconHorizontal;
var
  I, J: integer;
  lastPos: TPoint;
begin
  DoubleBuffered := true;
  J := 1;
  lastPos.X := 16;
  lastPos.Y := 16;

  for i:=0 to Self.ComponentCount - 1 do
    if self.Components[i] is Timage then
    begin
      lastPos.X := ( (self.Components[i] as Timage).Width ) * J ;
      (self.Components[i] as Timage).Left := lastPos.X;
      (self.Components[i] as Timage).Top  := lastPos.Y;
      inc(j);
    end;
end;

procedure TFrmPrincipal.FormCreate(Sender: TObject);
var
  I: integer;
  F: TInifile;
begin
  if not FileExists('c:\icones.ini') then
    Exit;

  F := Tinifile.Create('c:\icones.ini');
  for i:=0 to Self.ComponentCount - 1 do
    if self.Components[i] is Timage then
    begin
       (self.Components[i] as Timage).Left := F.ReadInteger((self.Components[i] as Timage).name,'Left', 0);
       (self.Components[i] as Timage).top := F.ReadInteger((self.Components[i] as Timage).name,'top', 0);
    end;
  F.Free;
end;

procedure TFrmPrincipal.IcoClienteDblClick(Sender: TObject);
begin
  if not Capturando then
    showmessage('oi');
end;

procedure TFrmPrincipal.FormDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  accept := Source is TMenuItem;
end;

procedure TFrmPrincipal.FormDragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  if Source is TMenuItem then
    with TImage.Create(Self) do
    begin
      Picture.Assign(image1.Picture);
      Left := x;
      Top := y;
    end;
end;

procedure TFrmPrincipal.VerticalEsquerdaClick(Sender: TObject);
begin
  FormAlignIcon(alLeft);
end;

procedure TFrmPrincipal.FormClose(Sender: TObject;
  var Action: TCloseAction);
var
  I: integer;
  F: TInifile;
begin
  F := Tinifile.Create('c:\icones.ini');
  for i:=0 to Self.ComponentCount - 1 do
  begin
    if self.Components[i] is Timage then
    begin
      F.WriteInteger( (self.Components[i] as Timage).name, 'Left', (self.Components[i] as Timage).Left);
      F.WriteInteger( (self.Components[i] as Timage).name, 'Top' , (self.Components[i] as Timage).Top );
    end;
  end;
  F.Free;
end;

procedure TFrmPrincipal.IcoClienteClick(Sender: TObject);
begin
  if not Capturando then
    Close;
end;

procedure TFrmPrincipal.VerticalDireitaClick(Sender: TObject);
begin
  FormAlignIcon(alRight);
end;

procedure TFrmPrincipal.HorizontalTopo1Click(Sender: TObject);
begin
  FormAlignIcon(alTop);
end;

procedure TFrmPrincipal.HorizontalBase1Click(Sender: TObject);
begin
  FormAlignIcon(alBottom);
  
end;

end.

